Chapter 18: Exercises

  1. 請說明在 MATLAB 和 C 之間,fscanf 指令的最大不同之處。
    Answer: 只要資料型態正確,MATLAB 的 fscanf 指令就會一直讀入資料,並把結果存到同一個向量回傳。
  2. 資料檔 test.txt 的內容如下: Sally Type1 12.34 45 Yes Joe Type2 23.54 60 No Bill Type1 34.90 12 No 請問 textread('test.txt','%s%*[^J]') 回傳的結果是?
  3. 資料檔 test.txt 的內容如下: Sally Type1 12.34 45 Yes 請問在輸入下列指令後,檔案內容為何?
    1. fid = fopen('test.txt', 'a'); fprintf(fid, 'hello'); fclose(fid);
    2. fid = fopen('test.txt', 'w'); fprintf(fid, 'hello'); fclose(fid);
  4. 請問用 csvread 指令來讀取資料時,除了檔案只能包含數值資料外,資料還必須使用下列哪個符號分開?
    1. 空白
    2. Tab
    3. 逗號
    4. 斜線
  5. The contents of a data file dataNum01.csv are shown as follows: 4, 5, 2 7, 2 6, 6, 7, 9 What is the value returned by the command csvread('dataNum01.csv')?
    Ans: ans = 4 5 2 0 7 2 0 0 6 6 7 9
  6. The contents of a data file dataNum01.csv are shown as follows: 4, 5, 2 7, 2 6, 6, 7, 9 What is the command that can be used to load this file directly?
  7. The contents of a data file dataNum01.dlm are shown as follows: 4 5 2 7 2 6 6 7 9 What is the command that can be used to load this file directly?
  8. A data file dataMix01.txt contains both text and numerical data, as shown next: Timmy OnlineGames 13 Annie Chatrooms 10 Roger Tennis 41 What is the most convenient command that can be used to read this file?
  9. A data file dataMix02.txt contains the field names as the first line, and both text and numerical data followed below, as shown next name favorite age Timmy OnlineGames 13 Annie Chatrooms 10 Roger Tennis 41 Write a function that can read this type of data, and return the output as a structure array with the specified field names.
  10. 假設有一整數矩陣 A,請寫一函數 intAsciiWrite.m,其功能是將此整數矩陣以 ASCII 的整數方式儲存於檔案之中,其用法如下:
    intAsciiWrite(A, fileName)
    例如當矩陣 A 的內容如下時: $$ A= \left[ \begin{matrix} 1 & 2 & 3 & 4\\ 5 & 6 & 7 & 8\\ \end{matrix} \right] $$ 則儲存於檔案之內容為 : 1 2 3 4 4 5 6 7
  11. 本題讓各位同學練習二進制檔案的讀寫。 請將 A = magic(10) 的資料以 uint8 的資料型態存入一個二進制檔案 mytest.bin,使用指令為 fwrite。 請再用 fread 指令將此魔方陣讀至工作空間的一個變數。

MATLAB程式設計:入門篇